$(function () {
    load();
    //批量删除
    $("#delMore").click(function () {
        if (confirm("确认要删除吗？这会删除歌手的所有歌曲")){
            //1.添加判断状态
            var flag = false;
            $.each($(".itemSelect"), function (i,j) {
                if (j.checked) flag = true;
            });
        }
        console.log("有复选框被选中："+flag)
        if (flag){
            $.post("/music/singer/deleteMoreSinger",$("#myForm").serialize(),{},"json");
            location.href = "singer_list.html";
        }
    });
});
function load(currentPage) {
    $.get("/music/singer/findSingerByPage",{currentPage:currentPage},function (data) {
        if (data.list!=null){
            //获取tbody
            $tbody = $("#singerMsg");
            //拼接
            var trlist = '';
            for (var i = 0; i < data.list.length; i++) {
                var tr ="<tr>" +
                    "       <td>" +
                    "           <label class='lyear-checkbox checkbox-primary'>" +
                    "           <input type='checkbox' name='sids' value='"+data.list[i].sid+"' " +
                    "                     id='"+data.list[i].sid+"' class='itemSelect'> <span></span>" +
                    "       </label>" +
                    "       </td>" +
                    "       <td>"+((data.currentPage-1)*data.pageSize+i+1)+"</td>" +
                    "       <td><img src='../"+data.list[i].singerimg+"' style='width: 300px'></td>" +
                    "       <td>"+data.list[i].singername+"</td>" +
                    "       <td>"+data.list[i].introduction+"</td>" +
                    "       <td>"+data.list[i].birthday+"</td>" +
                    "       <td>" +
                    "           <button class='btn btn-w-md btn-round btn-info'>" +
                    "           <a href='music_list.html?sid="+data.list[i].sid+" ' style='color: white;'>歌曲管理</a>" +
                    "           </button>" +
                    "       </td>" +
                    "       <td>" +
                    "           <div class='btn-group'>" +
                    "           <a class='btn btn-xs btn-default' href='singer_update.html?sid="+data.list[i].sid+"' title='编辑' data-toggle='tooltip'>" +
                    "               <i class='mdi mdi-pencil'></i>" +
                    "           </a>" +
                    "           <a class='btn btn-xs btn-default' href='javascript:deleteSinger("+data.list[i].sid+")' title='删除' data-toggle='tooltip'>" +
                    "               <i class='mdi mdi-window-close'></i>" +
                    "           </a>" +
                    "           </div>" +
                    "       </td>" +
                    "</tr>"
                trlist += tr;
            }
            //2.3 将liList添加到ul中
            $tbody.html(trlist);
            //4.处理页面 前5后4
            //4.1 开始页数 = 当前页数-5
            var start = data.currentPage-5;
            //4.2结束页数=当前页数+4
            var end = data.currentPage+4;
            //4.3判断前面不足5个的情况
            if (start<1){
                start=1;
                end=10;
            }
            //4.4判断后面不足4的情况
            if (end>data.totalPage){
                end = data.totalPage;
                start = end-9;
            }
            //4.5总页数不足10页
            if (data.totalPage<10){
                start = 1;
                end = data.totalPage;
            }
            //4.6拼接 ul -li
            $pageUl = $("#pageUl");
            //首页  cid = 3
            var pageList = '<li><a href="javaScript:load()">首页</a></li>';
            //上一页
            var prePage = data.currentPage - 1;
            if(prePage < 1){
                prePage = 1;
            }
            pageList += '<li >' +
                '<a href="javaScript:load('+prePage+')">上一页</a></li>';
            //拼接 页码 从start开始 end 结束
            for(var i = start ; i <= end; i++){
                if(i === data.currentPage){
                    pageList += '<li onclick="load('+i+')" class="active"><a href="javaScript:">'+i+'</a></li>';
                }else {
                    pageList += '<li onclick="load('+i+')" ><a href="javaScript:">'+i+'</a></li>';
                }
            }
            //下一页
            var nextPage = data.currentPage + 1;
            if (nextPage > data.totalPage){
                nextPage = data.totalPage;
            }
            pageList += '<li onclick="load('+nextPage+')" ><a href="javascript:;">下一页</a></li>';
            //末页
            pageList += '<li onclick="load('+data.totalPage+')" ><a href="javascript:;">末页</a></li>';
            //4.7 将pageList添加到ul中
            $pageUl.html(pageList);
        }
    },"json");
}
function deleteSinger(sid) {
    if (confirm("确定要删除吗？这会删除这位歌手的所有歌曲")) {
        $.post("/music/singer/deleteSingerAndMusicBySid",{sid:sid},function (data) {
            location.href="singer_list.html";
        });
    }
}
